#ifndef BPA_H
#define BPA_H

#include <vcg/complex/complex.h>
#include <vcg/complex/algorithms/update/bounding.h>
#include <vcg/complex/algorithms/update/topology.h>
#include <vcg/complex/algorithms/update/normal.h>
#include <vcg/complex/algorithms/update/flag.h>
#include <vcg/complex/algorithms/create/ball_pivoting.h>

using namespace vcg;
using namespace std;

class MyFace;
class MyVertex;

struct MyUsedTypes : public UsedTypes<Use<MyVertex>::AsVertexType, Use<MyFace>::AsFaceType>{};

class MyVertex  : public Vertex< MyUsedTypes, vertex::Coord3f, vertex::Normal3f, vertex::BitFlags, vertex::Mark>{};
class MyFace    : public Face  < MyUsedTypes, face::VertexRef, face::Normal3f, face::BitFlags > {};
class MyMesh    : public vcg::tri::TriMesh< vector<MyVertex>, vector<MyFace> > {};

//typedef typename MyMesh::VertexIterator VertexIterator;
//typedef typename MyMesh::FaceIterator FaceIterator;
//typedef typename MyMesh::VertexType VertexType;

//!  A Ball Pivot Algorithm class.
/*!
  It is class to construct mesh from points cloud by using Ball Pivot Algorithm.\n
  It uses a VCG lib, which base on the paper "The Ball-Pivoting Algorithm for Surface Reconstruction [F. Bernardinl 1999]"
*/

class BPA
{
public:
    //! A constructor.
    /*!
          Copy the imformation from CellObject to BPA, initial the MyMesh data.\n
          \param vertice a point of a array to store all vertice.
          \param normals a point of a array to store all normals.
          \param vertice_num the number of vertice.
          \param r the radius parameter of the ball-pivoting algorthm.
    */
    BPA(double *vertice, double *normals, int vertice_num, double r);

    //! Reconstruct mesh from points cloud by using Ball Pivot Algorithm.
    /*!
          \param face_num return the number of faces in the reconstected mesh.
    */
    void Reconstruction(int &face_num);

    //! Copy the faces of mesh from the MyMesh class to CellObject.
    /*!
          \param faceIndex a point of a array to store all face index.
    */
    void CopyFaceIndex(int *faceIndex);

    //! Get the vertex index of face.
    /*!
          \param m
          \param p
    */
    int GetIndexVertex(MyMesh &m, MyMesh::VertexType *p);
private:
    //! A MyMesh variable, the defination of this class is in VCG lib.
    MyMesh m;

    //! A float variable, the parameter of Ball Pivot Algorithm.
    float radius;

    //! A float variable, the parameter of Ball Pivot Algorithm.
    float clustering;
};

#endif // BPA_H
